<?php

namespace App\Models\Users;

use App\Models\BasicModel;
use App\Models\User;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class WithdrawalLog extends BasicModel
{
	use HasDateTimeFormatter;
    use SoftDeletes;

    protected $table = 'withdrawal_log';

    public function withdrawal($uid,$amount,$account,$platform): Model|WithdrawalLog
    {

        $user=User::query()->where('id',$uid)->first();
        if($user->earning<$amount) throw new \Exception('提现金额大于余额,最大提现 '.$user->earning);
        $price=bcdiv($amount,100,2);
        $data= [
            'uid' => $uid,
            'account' => $account,
            'amount' => $amount,
            'price' => $price,
            'platform' => $platform,
        ];
        $user->earning=bcsub($user->earning,$amount,2);
        $user->save();
        return self::create($data);

    }


    public function refuse($id){
        $log = self::find($id);
        (new MoneyLog())->add($log->uid,$log->amount,'提现审核失败退回',7);
        $log->status=2;
        return $log->save();
    }
}
